package com.seecoder.BlueWhale.controller;

import ch.qos.logback.core.recovery.ResilientFileOutputStream;
import com.seecoder.BlueWhale.service.CommodityService;
import com.seecoder.BlueWhale.util.SecurityUtil;
import com.seecoder.BlueWhale.vo.CommentVO;
import com.seecoder.BlueWhale.vo.CommodityVO;
import com.seecoder.BlueWhale.vo.PageVO;
import com.seecoder.BlueWhale.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.impl.common.ResolverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.rmi.activation.ActivationGroupDesc;
import java.util.List;

@RestController
@RequestMapping("/api/commodities")
@Slf4j
public class CommodityController {
    @Autowired
    CommodityService commodityService;

    @Autowired
    SecurityUtil securityUtil;


    @PostMapping("/create")
    public ResultVO<CommodityVO> create(@RequestBody CommodityVO commodityVO) {
        log.info("User[{}] Begin to create commodity {}.", securityUtil.getUserId(), commodityVO.getName());
        ResultVO<CommodityVO> ans =  ResultVO.buildSuccess(commodityService.create(commodityVO));
        log.info("User[{}]Commodity {} created.", securityUtil.getUserId(), commodityVO.getName());
        return ans;
    }


    @PostMapping("/change_quantity")
    public ResultVO<Boolean> addQuantity(@RequestParam Integer ID,
                                         @RequestParam Integer newQuantity) {
        log.info("User[{}] Begin to change quantity of commodity {}.", securityUtil.getUserId(), ID);
        ResultVO<Boolean> ans = ResultVO.buildSuccess(commodityService.changeQuantity(ID, newQuantity));
        log.info("User[{}] Quantity of commodity {} changed.", securityUtil.getUserId(), ID);
        return ans;
    }

    @PostMapping("/info")
    public ResultVO<CommodityVO> info(@RequestParam("id") int id) {
        log.info("Begin to get commodity info.");
        ResultVO<CommodityVO> ans = ResultVO.buildSuccess(commodityService.info(id));
        log.info("Commodity info got.");
        return ans;
    }

    @PostMapping("/get_comment")
    public ResultVO<List<CommentVO>> getComment(@RequestParam("id") int id) {
        log.info("Begin to get comment of commodity {}.", id);
        ResultVO<List<CommentVO>> ans = ResultVO.buildSuccess(commodityService.getComment(id));
        log.info("Comment of commodity {} got.", id);
        return ans;
    }

    @PostMapping("/search")
    public ResultVO<PageVO<CommodityVO>> searchCommodity(@RequestParam("max_price") Float maxPrice,
                                                         @RequestParam("min_price") Float minPrice,
                                                         @RequestParam("categories") List<Integer> categories,
                                                         @RequestParam("name") String name,
                                                         @RequestParam("page") int page,
                                                         @RequestParam("pageSize") int size) {
        log.info("Begin to search commodity.");
        ResultVO<PageVO<CommodityVO>> ans = ResultVO.buildSuccess(commodityService.searchCommodity(maxPrice,minPrice,categories,name, page, size));
        log.info("Commodity searched.");
        return ans;
    }
}